<template>
  <div style="padding: 40px;">
    <el-form ref="form" class="form" :model="form" label-width="60px" :inline="true">
      <el-form-item label="姓名">
        <el-input v-model="search.adminName"></el-input>
      </el-form-item>
      <el-form-item label="账号">
        <el-input v-model="search.phone"></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="search.roleCode" placeholder="请选择">
          <el-option v-for="i in role" :label="i.label" :value="i.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <!--        <el-input v-model="search.unitName"></el-input>-->
        <!-- <el-select :disabled="search.roleCode==''" v-model="search.unitName" placeholder="请选择">
          <el-option v-for="i in role2" :label="i.sysName" :value="i.sysName"></el-option>
        </el-select> -->
        <el-select v-model="search.status" placeholder="请选择">
          <el-option v-for="i in role2" :label="i.label" :value="i.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" class="search" type="primary" @click="searchC">搜索</el-button>
        <el-button icon="el-icon-plus" class="search" type="primary" @click="add">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="list" style="width: 100%" v-loading="loading">
      <el-table-column type="index" width="60" label="序号" />
      <el-table-column prop="adminName" label="姓名" />
      <el-table-column prop="phone" label="账号" />
      <!-- <el-table-column prop="unitAddr" label="部门" /> -->
      <el-table-column label="角色">
        <template #default="scope">
          {{ role.filter(i => i.value == scope.row.roleCode)[0].label }}
        </template>
      </el-table-column>
      <el-table-column prop="unitName" label="职务" />
      <el-table-column label="性别">
        <template #default="scope">
          <el-tag :type="scope.row.sex == 0 ? '' : 'success'">{{ scope.row.sex && '男' || '女' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template #default="scope">
          <el-popconfirm title="确定更改状态吗？" @onConfirm="confirmD(scope.row)">
            <el-button size="mini" slot="reference" :type="scope.row.status ? 'success' : 'danger'">{{ scope.row.status &&
              '启用' || '禁用' }}</el-button>
          </el-popconfirm>
          <!-- <el-tag :type="scope.row.status == '0' ? 'danger' : ''">{{ scope.row.status && '启用' || '禁用' }}</el-tag> -->
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="mini" icon="el-icon-edit" type="text" @click="edit(scope.row)">编辑</el-button>
          <el-button size="mini" icon="el-icon-view" type="text" @click="paw(scope.row)">修改密码</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="page.currentPage" :page-size="page.pageSize" layout="total, prev, pager, next, jumper"
      :total="page.total" @current-change="currentChange" />
    <el-dialog :title="addType == 1 && '新增' || '编辑'" :visible.sync="open" width="400px">
      <el-row>
        <el-form label-width="60px" v-if="addType == 3">
          <el-col :span="24">
            <el-form-item label="新密码">
              <el-input v-model="restPwd" />
            </el-form-item>
          </el-col>
        </el-form>
        <el-form label-width="60px" v-else>
          <el-col :span="24">
            <el-form-item label="姓名">
              <el-input v-model="form.adminName" />
            </el-form-item>
            <el-form-item label="账号">
              <el-input v-model="form.phone" />
            </el-form-item>
            <el-form-item label="密码" v-if="addType == 1">
              <el-input v-model="form.pwd" />
            </el-form-item>
            <el-form-item label="角色">
              <!-- <el-input v-model="form.roleCode" /> -->
              <el-select v-model="form.roleCode" placeholder="请选择" @change="sel">
                <el-option v-for="i in role.filter(ii => ii.value != '')" :label="i.label" :value="i.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="职务">
              <!-- <el-input v-model="form.unitName" /> -->
              <el-select :disabled="form.roleCode == ''" v-model="form.unitName" placeholder="请选择">
                <el-option v-for="i in role2" :label="i.sysName" :value="i.sysName"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="社区" v-if="form.roleCode == 'wgy'">
              <el-select :disabled="form.unitName == ''" v-model="sysvalue" placeholder="请选择">
                <el-option v-for="i in role3" :label="i.sysName" :value="i.sysName"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="性别">
              <el-radio-group v-model="sex" @input="v => sex = v">
                <el-radio label='1'>男</el-radio>
                <el-radio label="0">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import apis from '@/apis/api'

export default {
  data() {
    return {
      loading: false,
      restPwd: '',
      restId: 0,
      search: {
        phone: '', // 17779420699, //账号
        roleCode: '', // mj, //获取基础数据参数为2的时候获取的code值
        adminName: '', // 邓若, //姓名
        unitName: '', // 派单民警 //当为民警时，获取基础数据参数为2的时候获取的sysvalue值
        size: 10
      },
      form: {},
      sex: '1',
      addType: 1,
      open: false,
      list: [],
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 1,
      },
      role: [
        { label: '全部', value: '', roleCode: '' },
        { label: '民警', value: 'mj', roleCode: '2' },
        { label: '工作专班', value: 'tzb', roleCode: '2' },
        { label: '统战委员', value: 'tzwy', roleCode: '5' },
        { label: '各单位', value: 'gdw', roleCode: '3' },
        { label: '网格员', value: 'wgy', roleCode: '5' },
      ],
      role2: [{ label: '全部', value: ''},
        { label: '启用', value: '1'},
        { label: '禁用', value: '0' },
      ],
      role3: [],
      addStr: '',
      sysvalue: '',
      // 当为民警时，获取基础数据参数为2的时候获取的sysvalue值
      // 当为工作专班时，获取基础数据参数为2的时候获取的sysvalue值
      // 当为各单位时，获取基础数据参数为3的时候获取的sysvalue值
      // 当为统战委员时，获取基础数据参数为5的时候获取的sysvalue值+统战委员
      // 当为网格长时，获取基础数据参数为5的时候获取的sysvalue值+获取基础数据参数为4的时候获取的sysvalue值+网格长
    }
  },
  computed: {},
  mounted() {
    this.init()
  },
  methods: {
    sel(v) {
      if (v == '') return;
      this.form.unitName = '';
      let i = this.role.filter(i => i.value == v)[0].roleCode;
      if (i == 5) { this.addStr = this.role.filter(i => i.value == v)[0].label } else { this.addStr = '' };
      if (v == 'mj') {
        this.role2 = [{ sysName: '派单民警' }]
      } else if (v == 'tzb') {
        this.role2 = [{ sysName: '工作专班' }]
      } else if (v == 'wgy') {
        this.getSelect(4, 'wgy');
      } else {
        this.getSelect(i);
      }
    },
    // sysType  是  1：事件级别 2：管理类型 3：职能部门单位 4：社区 5：街道
    getSelect(v, t) {
      if (t != 'wgy') {
        apis.getInfo(v).then(res => {
          this.role2 = res.data;
        })
      } else if (t == 'wgy') {
        apis.getInfo(v).then(res => {
          this.role3 = res.data;
        })
      }
    },
    submit() {
      if (this.addType == 3) {
        if (this.restPwd == '') return this.$message.warning("请输入新密码");
        apis.restPwd({ id: this.restId, pwd: this.restPwd }).then(res => {
          this.$message.success("密码修改成功");
          this.open = false;
          this.init()
        })
      } else {
        if (this.form.adminName == '') return this.$message.warning("请输入姓名");
        if (this.form.phone == '') return this.$message.warning("请输入账号");
        if (this.form.roleCode == '') return this.$message.warning("请选择角色");
        if (this.form.unitName == '') return this.$message.warning("请选择职务");
        if (this.role3.length && this.sysvalue == '') return this.$message.warning("请选择职务2");
        this.form.unitName = this.form.unitName + this.sysvalue + this.addStr;

        apis.addOrEdit(this.form).then(res => {
          this.$message.success("操作成功");
          this.open = false;
          this.sysvalue = '';
          this.init()
        })
      }

    },
    add() {
      this.addType = 1;
      this.open = true;
      this.form = {
        phone: '', // 17779420699, //账号
        roleCode: '', // mj, //获取基础数据参数为2的时候获取的code值
        adminName: '', // 邓若, //姓名
        unitName: '',
        unitAddr: '',
        sex: 1
      }
    },
    edit(d) {
      this.addType = 2;
      this.open = true;
      this.form = d;
      this.sex = d.sex == 1 && '1' || '0';
    },
    paw(d) {
      this.addType = 3;
      this.restId = d.id;
      this.open = true;
    },
    confirmD(d) {
      apis.updateAdminStatus({ id: d.id, status: d.status == 0 && 1 || 0 }).then(res => {
        this.$message.success("状态修改成功");
        this.init()
      })
    },
    searchC() {
      this.page.currentPage = 1;
      this.init();
    },
    init() {
      this.search.current = this.page.currentPage;
      this.loading = true;
      apis.getAdminList(this.search).then(res => {
        let d = res.data;
        this.list = d.records;
        this.page.total = d.total;
        this.loading = false;
      })
    },
    currentChange(val) {
      this.page.currentPage = val
      this.init()
    },

  }
}
</script>
<style>
.el-pagination {
  float: right;
}
</style>
<style lang="scss" scoped></style>
